<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Weibo_fans extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('weibo_fans_model');
    }
    public function __destruct() {
        $this->db->close();
    }

    public function index()
    {
       // session_start();
        define( "WB_AKEY" , '1913157299' );
        define( "WB_SKEY" , 'e6c71c358076aa96fad4862fe8f0b752' );

        $c = new SaeTClientV2( WB_AKEY , WB_SKEY , "2.00snOcyBnb6TFC7df42acd6fqtiucB");
        $ms  = $c->home_timeline(); // done

        //$ms  = $c->bilateral_timeline();
        $uid_get = $c->get_uid();
        $uid = $uid_get['uid'];
        $user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
        
        $mutual_fans=$c->bilateral_ids($uid);  //获取互粉信息
        
        
        /*$this->db->query('truncate table weibo_info');  //暂时清空数据表：weibo_info
        foreach($mutual_fans['ids'] as $item)  //将所有互粉用户的ID和最新一条微博存入数据库
        {
            $user_info=$c->show_user_by_id($item);
            
            $name=$user_info['screen_name'];  //用户昵称
            $img_url=$user_info['profile_image_url'];  //头像链接
            $created_at=$user_info['created_at']; //微博发布时间
            $text=$user_info['status']['text']; //微博内容
            
            $status=$user_info['status'];
            
            if(isset ($status['retweeted_status']))//如果存在原微博
            {
                $retweeted_status=$status['retweeted_status'];
                
                $reretweeted_uid=$retweeted_status['user']['id']; //原微博用户ID;
                $retweeted_screen_name=$retweeted_status['user']['screen_name']; //原微博用户昵称
                $retweeted_created_at=$retweeted_status['created_at'];  //院微博发布时间
                $retweeted_txt=$retweeted_status['text'];   //原微博内容
            }
            else  //如果不存在原微博，即该微博为原创微博
            {
                $reretweeted_uid=''; //原微博用户ID;
                $retweeted_screen_name=''; //原微博用户昵称
                $retweeted_created_at='';  //院微博发布时间
                $retweeted_txt='';   //原微博内容
            }
            
            $this->weibo_fans_model->create_info($item,$name,$img_url,$created_at,$text,$reretweeted_uid,$retweeted_screen_name,$retweeted_created_at,$retweeted_txt); //uid,screen_name,img_url,text
        }*/
        
        
        //$weibo=$this->weibo_fans_model->get_info();
        $sql='select * from weibo_info order by rand() limit 5;';
        $weibo=$this->weibo_fans_model->get_info_sql($sql);
        
        //$pattern_url="|(http://t\.cn/\w+$)|";  //匹配短链接的正则表达式
        $pattern_url="/(http:\/\/[a-zA-Z0-9.\/]+)/";  //匹配短链接的正则表达式
        $pattern_at="|(@[\x{4e00}-\x{9fa5}A-Za-z0-9_]+)|u";        //匹配@某用户的正则表达式
        //$pattern_at="/@*[\s|:|：]/";
        
        
        
        //将读取的5条微博里的短链接转化成超链接
        foreach ($weibo as &$item)
        {
            if(preg_match_all($pattern_url, $item['weibo_text'], $matches,PREG_PATTERN_ORDER))  //如果能找到短链接
            {
                foreach($matches[0] as $match)   //将该微博内容中的每一条短链接转化成超链接
                {
                    $replace="<a style='text-decoration:none'  target=:_blank' href=".$match.">".$match."<a/>";    //完整的超链接
                    $item['weibo_text']=str_replace($match, $replace, $item['weibo_text']);   //用超链接替换博文中的短链接
                }
            }
        }
        
        //如果是非原创微博，则将原微博中的短链接也转化成超链接
        if( ! empty($item['retweeted_uid']))
        {
            foreach ($weibo as &$item) 
            {
                if(preg_match_all($pattern_url, $item['retweeted_txt'], $matches,PREG_PATTERN_ORDER))
                {
                    foreach($matches[0] as $match)
                    {
                        $replace="<a style='text-decoration:none' target:'_blank' href=".$match.">".$match."<a/>";
                        $item['retweeted_txt']=str_ireplace($match, $replace, $item['retweeted_txt']);
                    }
                }
            }
        }
         
        //将读取的微博中有@到某一用户的信息转化成超链接
        foreach($weibo as &$item)
        {
            if(preg_match_all($pattern_at, $item['weibo_text'], $matches,PREG_PATTERN_ORDER))  //如果存在@信息
            {
                foreach($matches[0] as $match)   //将该微博内容中的每一条@信息接转化成超链接
                {
                    $name=strtok($match, "@");  //将用户昵称识别出来
                    $user_infomation=$c->show_user_by_name( $name );   //以用户昵称为参数调用接口获取用户信息
                    $replace="<a style='text-decoration:none' target=:_blank' href=http://weibo.com/u/".(string)$user_infomation['id'].">@".$name."&nbsp<a/>";    //完整的超链接
                    $item['weibo_text']=str_replace($match, $replace, $item['weibo_text']);   //用超链接替换博文中的@信息
                }
            }
         }
        if( ! empty($item['retweeted_uid']))
        {
            foreach($weibo as &$item)
            {
                if(preg_match_all($pattern_at, $item['retweeted_txt'], $matches,PREG_PATTERN_ORDER))  //如果存在@信息
                {
                    foreach($matches[0] as $match)   //将该微博内容中的每一条@信息接转化成超链接
                    {
                        $name=strtok($match, "@");  //将用户昵称识别出来
                        $user_infomation=$c->show_user_by_name( $name );   //以用户昵称为参数调用接口获取用户信息
                        $replace="<a style='text-decoration:none' target:'_blank' href=http://weibo.com/u/".(string)$user_infomation['id'].">@".$name."&nbsp<a/>";    //完整的超链接
                        $item['retweeted_txt']=str_replace($match, $replace, $item['retweeted_txt']);   //用超链接替换博文中的@信息
                    }
                }
             }
        }
         
        $data['weibo']=$weibo;
        $data['c']=$c;
        $data['uid_me']=$uid;
        $data['user_message']=$user_message;
        //$data['count']=1;
        
        $this->load->view('weibo/weibo_fans',$data);
    }    
}
?>
